package com.rocksdata.mc.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.ctz.commonframe.ui.TitleBarActivity;
import com.ctz.commonframe.ui.widget.AbsAdapter;
import com.ctz.commonframe.ui.widget.AdapterViewHolder;
import com.ctz.commonframe.utils.GlideApp;
import com.ctz.commonframe.utils.SharedPrefrencesUtil;
import com.rocksdata.mc.business.BusinessManager;
import com.rocksdata.mc.business.CampusAppManager;
import com.rocksdata.mc.entity.CampusAppInfo;
import com.rocksdata.mc.R;
import com.rocksdata.mc.SharedPreferencesKey;
import com.rocksdata.mc.business.UserInfoManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

/**
 * Created by 陈堂正 on 2017/5/8.
 */
@ContentView(R.layout.activity_privacy_settings)
public class PrivacySettingsActivity extends TitleBarActivity {
    @ViewInject(R.id.lv_app_with_gesture_password)
    private ListView lvAppWithGesturePassword;
    @ViewInject(R.id.tv_new_gesture_password)
    private TextView tvNewGesturePassword;
    @ViewInject(R.id.tv_modify_gesture_password)
    private TextView tvModifyGesturePassword;

    private List<String> gesturePasswordAppCodeList;
    private AppWithGesturePasswordListAdapter adapter;
    private boolean hasPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setTitleMiddleText("隐私设置");

        List<CampusAppInfo> appList = BusinessManager.getApplicationManager().getAllAppList();
        gesturePasswordAppCodeList = CampusAppManager.getGesturePasswordAppCodes();
        adapter = new AppWithGesturePasswordListAdapter(this, appList);
        lvAppWithGesturePassword.setAdapter(adapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        String pwd = (String) SharedPrefrencesUtil.getAboutUser(UserInfoManager.getCurrentUserId(), SharedPreferencesKey.KEY_GESTURE_PASSWORD, "");
        hasPassword = !TextUtils.isEmpty(pwd);
        tvNewGesturePassword.setTextColor(getResources().getColor(hasPassword ? R.color.color_D9D9D9 : R.color.color_323232));
        tvModifyGesturePassword.setTextColor(getResources().getColor(hasPassword ? R.color.color_323232 : R.color.color_D9D9D9));
    }

    @Event(value = {R.id.layout_new_gesture_password, R.id.layout_modify_gesture_password})
    private void onClick(View view){
        switch (view.getId()) {
            case R.id.layout_new_gesture_password:
                if(!hasPassword){
                    startActivity(new Intent(PrivacySettingsActivity.this, GesturePasswordGuideActivity.class));
                }
                break;
            case R.id.layout_modify_gesture_password:
                if(hasPassword){
                    startActivity(new Intent(PrivacySettingsActivity.this, GesturePasswordUnlockActivity.class)
                            .putExtra("mode", GesturePasswordUnlockActivity.MODE_MODIFY_PASSWORD));
                }
                break;
        }
    }

    class AppWithGesturePasswordListAdapter extends AbsAdapter<CampusAppInfo>{

        AppWithGesturePasswordListAdapter(Context context, List<CampusAppInfo> datas) {
            super(context, datas, R.layout.item_app_with_gesture_password_list);
        }

        @Override
        public void convert(AdapterViewHolder holder, final CampusAppInfo data) {
            ImageView ivIcon = holder.getView(R.id.iv_application_icon);
            TextView tvName = holder.getView(R.id.tv_application_name);
            ToggleButton tbGesturePassword = holder.getView(R.id.tb_gesture_password);

            GlideApp.with(context).load(data.getIcon()).into(ivIcon);
            tvName.setText(data.getName());
            final boolean isChecked = gesturePasswordAppCodeList.contains(data.getCode());
            tbGesturePassword.setChecked(isChecked);
            tbGesturePassword.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (isChecked){
                        gesturePasswordAppCodeList.remove(data.getCode());
                    }else {
                        gesturePasswordAppCodeList.add(data.getCode());
                    }
                    CampusAppManager.saveGesturePasswordAppCodes(gesturePasswordAppCodeList);
                    adapter.notifyDataSetChanged();

                }
            });
        }
    }
}
